package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 员工管理Controller
 */
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;

    @GetMapping
    public Result find (String name,Short gender,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime begin,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime end,
                        @RequestParam Integer page,
                        @RequestParam Integer pagesize){
        return  Result.success(empService.find(name,gender,begin,end,page,pagesize));
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        empService.delete(ids);
        return Result.success();
    }
    @PostMapping
    public Result add(@RequestBody Emp emp){
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empService.add(emp);
        return Result.success();
    }
//  更新是动态的
    @PutMapping
    public Result update(@RequestBody Emp emp){
        emp.setUpdateTime(LocalDateTime.now());
        empService.update(emp);
        return Result.success();
    }

//    根据id查找
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        return Result.success(empService.findById(id));

    }

}
